﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;
using BLL;
using DAL;
using C1.Win.C1TrueDBGrid;

namespace Demo
{
    public partial class FrmDeparment : Form
    {
        DepartmentModel _departmentmodel = new DepartmentModel();
        DepartmentBLL _bll = new DepartmentBLL();
        private string deptNo = "";
        private string deptName = "";
        public FrmDeparment()
        {
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            for (int i = 0; i < this.c1tdgDepartment.Columns.Count; i++)
            {
                this.c1tdgDepartment.Columns[i].SortDirection = SortDirEnum.Ascending;
            }
            //设置复选框默认不选中
            this.txtDepartmentNo.ReadOnly = true;
            this.txtDepartmentName.ReadOnly = true;           
        }

        private void FrmEmpoloyee_Load(object sender, EventArgs e)
        {
            LoadData("","");
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        public void InitGrid()
        {
            this.c1tdgDepartment.Columns.Clear();
            SetUnbandColumns(c1tdgDepartment, 0, "部门名称", "DepartmentNo", 140, true);
            SetUnbandColumns(c1tdgDepartment, 1, "员工编码", "DepartmentName", 140, true);
            SetUnbandColumns(c1tdgDepartment, 2, "备注", "Remarks", 200, true);          
            c1tdgDepartment.SetActiveCell(-1, 0);
        }

        public void SetUnbandColumns(C1TrueDBGrid grid, int index, string caption, string datafield, int width, bool visible)
        {
            C1DataColumn dataColumn = new C1DataColumn();
            grid.Columns.Insert(index, dataColumn);
            dataColumn.Caption = caption;
            dataColumn.DataField = datafield;

            C1DisplayColumn discolDeviceName = grid.Splits[0].DisplayColumns[dataColumn];
            grid.Splits[0].DisplayColumns.RemoveAt(index);
            grid.Splits[0].DisplayColumns.Insert(index, discolDeviceName);
            discolDeviceName.Visible = visible;
            if (width > 0)
                discolDeviceName.Width = width;
            else
                discolDeviceName.AutoSize();
            discolDeviceName.Style.HorizontalAlignment = AlignHorzEnum.Center;
            discolDeviceName.Style.VerticalAlignment = AlignVertEnum.Center;
            discolDeviceName.HeadingStyle.VerticalAlignment = AlignVertEnum.Center;
            discolDeviceName.HeadingStyle.HorizontalAlignment = AlignHorzEnum.Center;
            discolDeviceName.HeadingStyle.Borders.BorderType = BorderTypeEnum.Flat;
        }

        /// <summary>
        /// 显示行号
        /// </summary>
        /// <param name="dgv"></param>
        public void ShowIndex(DataGridView dgv)
        {
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                dgv.Rows[i].HeaderCell.Value = (i + 1).ToString();  //HeaderCell获取或设置行的标头单元格            
            }
        }
 
        /// <summary>
        /// 查询部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnSearch_Click(object sender, EventArgs e)
        {
            GetQuery();
            LoadData(deptNo, deptName);
        }

        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnAdd_Click(object sender, EventArgs e)
        {
            FrmDeparmentAdd addfrm = new FrmDeparmentAdd(); 
            addfrm.Width = 455;
            addfrm.ShowDialog();
            if (addfrm.DialogResult == DialogResult.OK)
            {
                //重新加载数据
                LoadData("", "");
            } 
        }

        /// <summary>
        /// 修改部门信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnUpdate_Click(object sender, EventArgs e)
        {
            DeptUpdate();
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnDel_Click(object sender, EventArgs e)
        {
            DeptDelete();
            GetQuery();
            LoadData(deptNo, deptName);
        }       

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="conn"></param>
        /// <returns></returns>
        public void LoadData(string DepartmentNo, string DepartmentName)
        {
            IEnumerable<DepartmentModel> departmentlist = _bll.GetModelListByNoAndName(DepartmentNo, DepartmentName);
            this.c1tdgDepartment.DataSource=departmentlist;
            InitGrid();
        }

        /// <summary>
        /// 删除部门信息
        /// </summary>
        public void DeptDelete()
        {
            if (this.c1tdgDepartment.SelectedRows.Count > 0)
            {
                //DepartmentModel model = this.c1tdgDepartment.SelectedRows.DataBoundItem as DepartmentModel;
                DepartmentModel model =this.c1tdgDepartment[this.c1tdgDepartment.RowBookmark(this.c1tdgDepartment.Row)] as DepartmentModel;
                string msg = "确定删除部门编号是" + model.DepartmentNo + "的这一项吗？";
                if ((int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) != 1)
                {
                    return;
                }
                else
                {
                    _bll.Delete(model.Id);
                }
            }
            else
            {
                MessageBox.Show("请选中一项进行删除");
            }            
        }

        /// <summary>
        /// 增加
        /// </summary>
        public void DeptUpdate()
        {
            if (this.c1tdgDepartment.SelectedRows.Count > 0)
            {
                DepartmentModel model = this.c1tdgDepartment[this.c1tdgDepartment.RowBookmark(this.c1tdgDepartment.Row)] as DepartmentModel;
                FrmDeparmentAdd updatefrm = new FrmDeparmentAdd(model.Id);
                updatefrm.Width = 455;
                updatefrm.ShowDialog();
                if (updatefrm.DialogResult == DialogResult.OK)
                {
                    //重新加载数据
                    LoadData("", "");
                } 
            }
            else
            {
                MessageBox.Show("请选中一条数据进行修改");
            }
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        public void GetQuery()
        {
            deptNo = "";
            deptName = "";
            if (this.cbDepartmentNo.Checked)
            {
                deptNo = this.txtDepartmentNo.Text;
            }
            if (this.cbDepartmentName.Checked)
            {
                deptName = this.txtDepartmentName.Text;
            }
        }

        /// <summary>
        /// cbDepartmentNo点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbDepartmentNo_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbDepartmentNo.Checked == false)
            {
                this.txtDepartmentNo.ReadOnly = true;
            }
            else
            {
                this.txtDepartmentNo.ReadOnly = false;
            }
        }

        /// <summary>
        /// cbDepartmentName点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbDepartmentName_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbDepartmentName.Checked == false)
            {
                this.txtDepartmentName.ReadOnly = true;
            }
            else
            {
                this.txtDepartmentName.ReadOnly = false;
            }
        }

        private void c1tdgDepartment_MouseClick(object sender, MouseEventArgs e)
        {
            int row = this.c1tdgDepartment.RowContaining(e.Y);
            if (row != -1)
            {
                int index = this.c1tdgDepartment.SelectedRows.IndexOf(row);
                if (index != -1)
                    this.c1tdgDepartment.SelectedRows.RemoveAt(index);
                else
                    this.c1tdgDepartment.SelectedRows.Add(row);
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        

    }


}
